﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI;
using DevExpress.Web.ASPxHeadline;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxCallback;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;
using LMD.Lib.Oracle;

public partial class CCView : ICPRelationshipPage
{
    public CCData m_ccData = new CCData();
    public CCQuestionData m_ccqData;
    public List<CCQuestionData> m_lstccqData = new List<CCQuestionData>();
    public DataTable m_dtQuestion = new DataTable();

    #region Properties
    public int m_iCCID
    {
        get
        {
            if (!String.IsNullOrEmpty(Request.QueryString["ccid"]))
            {
                return Convert.ToInt32(Request.QueryString["ccid"]);
            }
            else
            {
                return -1;
            }
        }
    }
    #endregion Properties

    #region Event Handlers

    protected override void Page_Load(object sender, EventArgs e)
    {
        m_lsPermittedRoles.Add(ICPUserRole.CAR);
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: Relationship, Courtesy Call - View";
        if (!IsPostBack && !IsCallback)
        {
            m_dtQuestion = InitCCQuestionData();
            InitCallData();
        }
    }
    protected void cbkView_Callback(object sender, CallbackEventArgs e)
    {
        System.Threading.Thread.Sleep(KGlobal.AJAX_WAIT_TIME);
        e.Result = HandleView(e.Parameter);
    }
    #endregion Event Handlers

    #region Custom Methods
    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    protected void InitCallData()
    {
        Cached.CourtesyCallTable cachedCC = new Cached.CourtesyCallTable();
        DataTable dtCallData = cachedCC.GetCallDetailById(m_iCCID);

        if (dtCallData.Rows.Count > 0)
        {
            m_ccData.iCCID = m_iCCID;

            if (dtCallData.Rows[0]["CONTACT_PERSON_ID"] != DBNull.Value)
            {
                m_ccData.iContactPersonID = Convert.ToInt32(dtCallData.Rows[0]["CONTACT_PERSON_ID"]);
            }

            if (dtCallData.Rows[0]["NEW_CONTACT_PERSON_ID"] != DBNull.Value)
            {
                m_ccData.iNewContactPersonID = Convert.ToInt32(dtCallData.Rows[0]["NEW_CONTACT_PERSON_ID"]);
            }
            if (dtCallData.Rows[0]["CALL_STATUS"] != DBNull.Value)
            {
                m_ccData.sxCallStatus = dtCallData.Rows[0]["CALL_STATUS"].ToString();
            }
            if (dtCallData.Rows[0]["NOTE"] != DBNull.Value)
            {
                m_ccData.sxNote = dtCallData.Rows[0]["NOTE"].ToString();
            }
            if (dtCallData.Rows[0]["CANCELATION_REASON"] != DBNull.Value)
            {
                m_ccData.sxReason = dtCallData.Rows[0]["CANCELATION_REASON"].ToString();
            }
            if (dtCallData.Rows[0]["PLANNED_DATE"] != DBNull.Value)
            {
                m_ccData.dtmPlannedDate = Convert.ToDateTime(dtCallData.Rows[0]["PLANNED_DATE"]);
            }
            if (dtCallData.Rows[0]["REALIZATION_DATE"] != DBNull.Value)
            {
                m_ccData.dtmRealizationDate = Convert.ToDateTime(dtCallData.Rows[0]["REALIZATION_DATE"]);
            }
            if (dtCallData.Rows[0]["COMPANY_NAME"] != DBNull.Value)
            {
                m_ccData.sxCompanyName = dtCallData.Rows[0]["COMPANY_NAME"].ToString();
            }
            if (dtCallData.Rows[0]["NAME"] != DBNull.Value)
            {
                m_ccData.sxContactPersonName = dtCallData.Rows[0]["NAME"].ToString();
            }
            if (dtCallData.Rows[0]["CUSTOMER_ID"] != DBNull.Value)
            {
                m_ccData.iCustomerID = Convert.ToInt32(dtCallData.Rows[0]["CUSTOMER_ID"]);
            }
            if (dtCallData.Rows[0]["CREATE_BY_NAME"] != DBNull.Value)
            {
                m_ccData.sxCreateBy = dtCallData.Rows[0]["CREATE_BY_NAME"].ToString();
            }
			if (dtCallData.Rows[0]["T_FILE_UPLOAD_ID"] != DBNull.Value)
            {
                m_ccData.iFileUploadId = Convert.ToInt32(dtCallData.Rows[0]["T_FILE_UPLOAD_ID"]);
            }
            if (dtCallData.Rows[0]["ORIGINAL_FILENAME"] != DBNull.Value)
            {
                m_ccData.sxFileName = dtCallData.Rows[0]["ORIGINAL_FILENAME"].ToString();
            }
        }
    }
    protected DataTable InitCCQuestionData()
    {
        Cached.CourtesyCallTable cachedCC = new Cached.CourtesyCallTable();
        return cachedCC.GetCCQuestionDetailById(m_iCCID);
    }

    private string HandleView(string sxParam)
    {
        String[] sxSplitted = sxParam.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);

        String sxOutput = String.Empty;
        int iCPID = -1;
        bool isNewCP = sxSplitted[2].ToString().ToUpper().Equals("Y");
        switch (sxSplitted[0].ToUpper())
        {
            case "SHOW":
                iCPID = Convert.ToInt32(sxSplitted[1]);
                ContactInfoData cpInfoData = new ContactInfoData();
                Cached.ContactPersonTable cachedCP = new Cached.ContactPersonTable();
                cpInfoData = cachedCP.GetContactInfoData(iCPID, isNewCP);
                sxOutput = CCManager.BuildEditor_CPInfoPopUp(iCPID.ToString(), cpInfoData);
                break;
        }

        return sxOutput;
    }
    #endregion Custom Methods
}
